package com.rim.samples.device.helloworlddemo;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.RecordControl;

import net.rim.device.api.media.control.AudioPathControl;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class SendThread extends Thread implements PlayerListener
{
    static String sDestUri;
    
    private Player _player;
    
    private RecordControl _rcontrol;
    
    private DatagramConnection mConnection;
    
    private DatagramConnection createDatagramCnx(int port) throws IOException
    {
        String url = "datagram://:" + port;
        if ((CoverageInfo.getCoverageStatus(RadioInfo.WAF_WLAN, true) & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT)
        {
            url += "/ ;interface=wifi";
        }
        return (DatagramConnection) Connector.open(url);
    }
    
    private OutputStream _output = new OutputStream()
    {
        private byte[] mTransBuffer;
        
        public void write(byte[] buffer, int offset, int count)
        {
        	mTransBuffer = new byte[count];
        	
            System.arraycopy(buffer, offset, mTransBuffer, 0, count);
            Datagram dg;
			try 
			{
				dg = mConnection.newDatagram(mTransBuffer, count, sDestUri);
				mConnection.send(dg);
			} 
			catch (IOException e) 
			{
				Log.d("Send","sent data Exception!");
				e.printStackTrace();
			}
        }
        
        public void write(byte[] buffer) throws IOException
        {
            write(buffer, 0, buffer.length);
        }
        
        public void write(int b) throws IOException
        {
            Log.d("Send","not implement");
            
        }
    };
    
    public SendThread(String address)
    {
        sDestUri = "datagram://" + address + ":38858";
    }
    
    
    public void run()
    {
        try
        {
            mConnection = createDatagramCnx(38858);
            _player = Manager.createPlayer("capture://audio?encoding=pcm");
            
            _player.realize();
            
            // Get the RecordControl, configure the record stream,
            _rcontrol = (RecordControl) _player.getControl("RecordControl");
            
            // Create a ByteArray OutputStream to record the audio stream.
            _rcontrol.setRecordStream(_output);
            _rcontrol.startRecord();
            AudioPathControl  lPathCtr = (AudioPathControl) _player.getControl("net.rim.device.api.media.control.AudioPathControl");
            lPathCtr.setAudioPath(AudioPathControl.AUDIO_PATH_HANDSET);

            _player.start();
        }
        catch (final Exception e)
        {
            UiApplication.getUiApplication().invokeAndWait(new Runnable()
            {
                public void run()
                {
                    Dialog.inform(e.toString());
                }
            });
        }
    }
    
    public void stop()
    {
        try
        {
            _rcontrol.commit();
            _output.close();
            _player.close();
            
            mConnection.close();
        }
        catch (Exception e)
        {
            synchronized (UiApplication.getEventLock())
            {
                Dialog.inform(e.toString());
            }
        }
    }
    
    public void playerUpdate(Player player, String event, Object eventData)
    {
        
    }
}
